﻿using GameFramework;
using System;
using System.Collections.Generic;
using System.IO;
using UnityGameFramework.Runtime;

namespace ProjectMG
{
    public static class LocalizationExtension
    {
        private static readonly Dictionary<string, bool> s_DictionaryLoadedFlags = new Dictionary<string, bool>();

        public static bool IsAllDictionariesLoaded(this LocalizationComponent localizationComponent)
        {
            if (GameEntry.BuiltinData.UseDictionaryBytes)
            {
                // 使用二进制字典表不需要预加载资源了，使用哪条字典加载哪条字典
                return true;
            }

            if (s_DictionaryLoadedFlags.Count <= 0)
            {
                return false;
            }

            foreach (KeyValuePair<string, bool> dictionaryLoadedFlag in s_DictionaryLoadedFlags)
            {
                if (!dictionaryLoadedFlag.Value)
                {
                    return false;
                }
            }

            return true;
        }

        public static void SetDictionaryLoadedFlag(this LocalizationComponent localizationComponent, string dictionaryName)
        {
            if (s_DictionaryLoadedFlags.ContainsKey(dictionaryName))
            {
                s_DictionaryLoadedFlags[dictionaryName] = true;
            }
            else
            {
                Log.Warning("Not load dictionary '{0}' yet.", dictionaryName);
            }
        }

        public static void LoadDictionaries(this LocalizationComponent localizationComponent)
        {
            s_DictionaryLoadedFlags.Clear();
            if (GameEntry.BuiltinData.UseDictionaryBytes)
            {
                string assetName = AssetUtility.GetBinaryDictionaryAsset();
                //s_DictionaryLoadedFlags.Add(assetName, false);
                GameEntry.Localization.ReadData(assetName, Constant.AssetPriority.DictionaryAsset, GameEntry.BuiltinData.UseDictionaryBytes);
            }
            else if (GameEntry.Base.EditorResourceMode)
            {
                string dictionaryListAssetName = GameEntry.Resource.GetBinaryPath(AssetUtility.GetDictionaryListAsset());
                string[] dictionaryNames = Utility.Converter.GetString(File.ReadAllBytes(dictionaryListAssetName)).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string dictionaryName in dictionaryNames)
                {
                    LoadDictionary(dictionaryName);
                }
            }
            else
            {
                Log.Error("Read dictionary from file is not support without editor resource mode.");
            }
        }

        private static void LoadDictionary(string dictionaryName)
        {
            if (string.IsNullOrEmpty(dictionaryName))
            {
                Log.Warning("Dictionary name is invalid.");
                return;
            }

            string assetName = AssetUtility.GetDictionaryAsset(dictionaryName);
            s_DictionaryLoadedFlags.Add(assetName, false);
            GameEntry.Localization.ReadData(assetName, Constant.AssetPriority.DictionaryAsset, GameEntry.BuiltinData.UseDictionaryBytes);
        }
    }
}
